package com.unisci.android.dialog;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.unisci.android.R;
import com.unisci.android.view.PickerView;
import com.unisci.android.view.PickerView.onSelectListener;

public class TimeDialog extends Dialog implements OnClickListener {

	private static final String TAG = "PCR";
	private PickerView mMinPickerView;
	private PickerView mSecPickerView;

	private Button mSureButton;
	private Button mCancelButton;

	private onTimePickedListener mListener;
	private int mTag;
	private int min, sec;

	public TimeDialog(Context context) {
		super(context);
		initViews();
		setTitle("设置时间");
	}

	public void setTag(int tag) {
		mTag = tag;
	}

	private void initViews() {
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.dialog_time, null);

		mMinPickerView = (PickerView) view.findViewById(R.id.picker_min);
		List<String> minites = new ArrayList<String>();
		for (int i = 0; i < 60; i++) {
			minites.add(i < 10 ? "0" + i : "" + i);
		}
		mMinPickerView.setData(minites);
		mMinPickerView.setOnSelectListener(new onSelectListener() {

			@Override
			public void onSelect(String text) {
				// TODO Auto-generated method stub
				Log.d(TAG, "select " + text);
				min = Integer.valueOf(text);
			}
		});

		mSecPickerView = (PickerView) view.findViewById(R.id.picker_sec);
		List<String> seconds = new ArrayList<String>();
		for (int i = 0; i < 60; i++) {
			seconds.add(i < 10 ? "0" + i : "" + i);
		}
		mSecPickerView.setData(seconds);
		mSecPickerView.setOnSelectListener(new onSelectListener() {

			@Override
			public void onSelect(String text) {
				// TODO Auto-generated method stub
				Log.d(TAG, "select " + text);
				sec = Integer.valueOf(text);
			}
		});
		mMinPickerView.setSelected(0);
		min = 0;
		sec = 30;

		mSureButton = (Button) view.findViewById(R.id.dialog_ok);
		mCancelButton = (Button) view.findViewById(R.id.dialog_cancel);
		mSureButton.setOnClickListener(this);
		mCancelButton.setOnClickListener(this);

		super.setContentView(view);
	}

	public void setOnTimePickedListener(onTimePickedListener listener) {
		mListener = listener;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.dialog_ok) {
			dismiss();
			mListener.onTimePicked(mTag, min, sec);
		} else if (v.getId() == R.id.dialog_cancel) {
			dismiss();
		}
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
	}

	public interface onTimePickedListener {
		public void onTimePicked(int tag, int min, int sec);
	}

}
